var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_info: null,
    order_list: {},
    options:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    const openid = app.globalData.openid;
    this.setData({
      options:options
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Goods/tuanShare/openid/'+openid+'/id/'+options.id, function(res){
      wx.setNavigationBarTitle({
        title: res.data.goods_info.title,
      })
      that.setData({
        goods_info: res.data.goods_info,
        order_list: res.data.order_list,
        is_join: res.data.is_join
      })
    })
  },
  pay: function (e) {
    const order_type = e.currentTarget.dataset.type;
    var post_data = {};
    post_data.id = this.data.order_list[0].goods_id;
    post_data.openid = app.globalData.openid;
    post_data.tuan_first_id = this.data.order_list[0].id;
    if (order_type) {
      post_data.type = order_type;
    }

    app.ihttpPost("https://www.58zltc.com/api.php/Goods/addGoodsOrder", post_data, function (res) {
      if (res.data.err_code == 400) {
        app.iModel(res.data.err_msg);
      } else {
        var data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              if (order_type == 'pin') {
                var url = '/pages/shop/pintuan/share';
              } else {
                var url = '/pages/mine/order/order';
              }
              wx.navigateTo({
                url: url,
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    const openid = app.globalData.openid;
    app.ihttpGet('https://www.58zltc.com/api.php/Goods/tuanShare/openid/' + openid + '/id/' + this.data.options.id, function (res) {
      wx.setNavigationBarTitle({
        title: res.data.goods_info.title,
      })
      that.setData({
        goods_info: res.data.goods_info,
        order_list: res.data.order_list,
        is_join: res.data.is_join
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const data = this.data.order_list[0];
    return {
      title: '【仅剩' + (data.tuan_need_num - data.tuan_pay_num)+'人】快来'+data.price+'元拼'+data.title,
      path: '/pages/shop/pintuan/share?id='+data.id,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功！',
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})